package kr.saeildamil.project.secretary.fragment;

import kr.saeildamil.project.secretary.CurrentTime;
import kr.saeildamil.project.secretary.Main;
import kr.saeildamil.project.secretary.R;
import kr.saeildamil.project.secretary.DB.DBHelper;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class NewSchedule extends Fragment{
	static TextView time;
	static Button complete;
	static int pickHour;
	static int pickMinute;
	static String hourString;
	static String minString;
	static DBHelper dbHelper;
	static SQLiteDatabase db;
	static String name=null;
	static String date;
	static String task;
	static EditText detail;
	static String todayDetail;
	static Spinner spin;
	static ArrayAdapter<CharSequence> adspin;
	static String TAG="Newschedule";
	CurrentTime currenttime;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.newschedule, container, false);
		
		time=(TextView)root.findViewById(R.id.current_time);
		detail=(EditText) root.findViewById(R.id.details);
		spin=(Spinner) root.findViewById(R.id.task_spinner);
		complete = (Button) root.findViewById(R.id.newschedule_complete);
		
		/*SharedPreferences pref=getSharedPreferences("Secretary", 0);
		name=pref.getString("Selecteduser", null);
		Log.d(TAG, "name : "+name);*/
		Main main=new Main();
		date=main.getMainDate();
		
		adspin=ArrayAdapter.createFromResource(getActivity(), R.array.task, android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);
		spin.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				task=adspin.getItem(position).toString();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		//time text에 timepicker listener을 달아줌
		currenttime=new CurrentTime();
		time.setText(currenttime.currenttime);
		time.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getActivity(), "timepicker호출", 0).show();
				Log.d(TAG, "timepicker호출");
				new TimePickerDialog(getActivity(), mTimeSetListener, pickHour, pickMinute, false).show();
			}
		});
		
		//완료버튼
		complete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dbHelper=new DBHelper(getActivity());
				db=dbHelper.getWritableDatabase();
				Log.d(TAG, "mainDate : "+date);
				todayDetail=detail.getText().toString();
				
				//사용자가 시간 선택없을 경우 null 값이 입력되기 때문에 강제로 시간을 입력해준다 
				if(hourString==null){
					hourString=currenttime.currentHour;
					minString=currenttime.currentMinute;
				}
				
				db.execSQL("INSERT INTO schedule VALUES(null,'"+date+"','"+hourString+"','"+minString+"','"+task+"','"+todayDetail+"');");
				dbHelper.close();

				complete.setText("저장완료");

				Intent intent=new Intent(getActivity(), Main.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				startActivity(intent);
			}
		});
		return root;
	}

	//timepickerdialog listener 구현
	TimePickerDialog.OnTimeSetListener mTimeSetListener=new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			pickHour=hourOfDay;
			pickMinute=minute;
			hourString=String.valueOf(pickHour);
			minString=String.valueOf(pickMinute);

			if(hourString.length()==1){
				hourString="0"+hourString;
			}
			if(minString.length()==1){
				minString="0"+minString;
			}

			time.setText(hourString+":"+minString);
		}
	};
}

